package cn.rxmind.dlicense.core.validator;

import cn.rxmind.dlicense.core.base.DLicenseContent;
import cn.rxmind.dlicense.core.rule.SpringApplicationNameRule;
import cn.rxmind.dlicense.core.utils.SpringEnvironmentUtil;
import com.google.common.collect.Sets;
import lombok.Setter;

/**
 * 子站编码校验器
 *
 * @author renxin
 * @email renxinzhiliangzhi@163.com
 * @date 2021/4/13 2:14 下午
 */
public class SpringApplicationNameLicenseValidator extends AbstractExitJVMLicenseValidator {

    private static final String SPRING_APPLICATION_NAME_KEY = "spring.application.name";

    @Override
    public String description() {
        return "子站编码校验";
    }

    @Override
    public ValidateResult validateResult(DLicenseContent content) {

        SpringApplicationNameValidateResult result = new SpringApplicationNameValidateResult();
        if(content.getSpringApplicationNameRule() != null){
            String subSiteCode = SpringEnvironmentUtil.getProperty(SPRING_APPLICATION_NAME_KEY);
            SpringApplicationNameRule springApplicationNameRule = content.getSpringApplicationNameRule();
            result.setValid(springApplicationNameRule.test(Sets.newHashSet(subSiteCode)));
        }

        return result;
    }

    @Setter
    static class SpringApplicationNameValidateResult implements ValidateResult{

        private boolean valid;

        @Override
        public Class<SpringApplicationNameLicenseValidator> getLicenseValidator() {
            return SpringApplicationNameLicenseValidator.class;
        }

        @Override
        public boolean valid() {
            return this.valid;
        }

        @Override
        public String getDescription() {

            return valid() ? "valid" : "子站编码不允许";
        }
    }

}
